#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2010;

typedef struct {int v,nxt;}edge;

int f[MAXN],h[MAXN],sz[MAXN],a[MAXN],dis[MAXN],n,ans = 0x3f3f3f3f,cnt,son[MAXN];
edge e[MAXN<<1];

void link(int x,int y){e[++cnt] = (edge){y,h[x]};h[x] = cnt;}

void dfs(int x,int p){
	sz[x] = a[x]; dis[x] = f[x] = 0; son[x] = 0;
	for(int i = h[x];i;i = e[i].nxt){
		int y = e[i].v;
		if(y != p) {
			dfs(y,x);
			dis[y] += sz[y];
			sz[x] += sz[y];
			dis[x] += dis[y];
			if(dis[y] > dis[son[x]]) son[x] = y;
		}
	}
	if(!son[x]) {
		f[x] = 0;
		return;
	}
	if(dis[x] >= 2 * dis[son[x]]) f[x] = dis[x] / 2;
	else f[x] = dis[x] - dis[son[x]] + min(f[son[x]]*2,2 * dis[son[x]] - dis[x])/2;
}
			
int main (){
	scanf("%d",&n);
	for(int i = 1;i <= n;i++) scanf("%1d",&a[i]);
	for(int i = 1;i < n;i++){
		int x,y;scanf("%d %d",&x,&y);
		link(x,y);link(y,x);
	}
	for(int i = 1;i <= n;i++){
		dfs(i,0);
		if(dis[i] & 1) continue;
		if(dis[i] / 2 == f[i]) ans = min(ans,dis[i]/2);
	}
	if(ans >= 0x3f3f3f3f) ans = -1;
	printf("%d\n",ans);
	return 0;
}
